package cm.example.shoppingplatformspringboot.mapper;


import cm.example.shoppingplatformspringboot.entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {
    // 新增：根据账号获取用户
    @Select("select * from wn_user where account = #{account}")
    User getUserByAccount(String account);

    // 根据手机号获取用户
    @Select("select * from wn_user where telphone = #{telphone}")
    User getUserByTelphone(String telphone);

    // 新增：插入用户
    @Insert("INSERT INTO wn_user (account, password, telphone, email, avatar, reg_time, status, points, money) VALUES (#{account}, #{password}, #{telphone}, #{email}, #{avatar}, NOW(), '1', 0, 0.00)")
    int insertUser(User user);

    // 新增：更新用户信息
    @Update("UPDATE wn_user SET account=#{account}, password=#{password}, email=#{email}, telphone=#{telphone}, avatar=#{avatar} WHERE id=#{id}")
    int updateUser(User user);

    // 新增：根据ID获取用户
    @Select("SELECT * FROM wn_user WHERE id = #{id}")
    User getUserById(Integer id);

    /**
     * 更新用户登录信息
     *
     * @param account 用户账号（精确匹配）
     * @param ip      登录IP
     * @implNote 同时更新last_login_time为当前时间
     */
    @Update("update wn_user set last_login_time = NOW(), last_login_ip = #{ip} where account = #{account}")
    void updateUserLoginInfo(@Param("account") String account, @Param("ip") String ip);

    // 新增：根据手机号获取用户数量
    @Select("SELECT COUNT(*) FROM wn_user WHERE telphone = #{phone}")
    int countByTelphone(String phone);

    // 新增：根据邮箱获取用户数量
    @Select("SELECT COUNT(*) FROM wn_user WHERE email = #{email}")
    int countByEmail(String email);
}
